package com.android.aishangauction.activity.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.app.BaseActivity;
import com.android.aishangauction.model.data;
import com.android.aishangauction.model.dataCallback;
import com.android.aishangauction.model.member.AddressListData;
import com.android.aishangauction.model.member.AddressListDataCallback;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.android.aishangauction.R.id.main_top_title_editaddress;

/**
 * Created by Administrator on 2017/9/5.
 */

public class TakeOverAddressActivity extends BaseActivity {


    @BindView(R.id.main_top_back)
    ImageView back;
    @BindView(main_top_title_editaddress)
    TextView maintop_title_editaddress;
    @BindView(R.id.listView)
    NestFullListView listView;
    @BindView(R.id.address_add)
    TextView address_add;
    @BindView(R.id.no_data_address)
    LinearLayout noData;


    private boolean networkConnected;
    private FindPwActivity.TimeCount time;
    private Activity mcontext;
    private boolean isEdit=true;
    ArrayList<AddressListData.AddressData> addressdata = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_takeoveraddress);
        mcontext=this;
        ButterKnife.bind(this);
        maintop_title_editaddress.setText("编辑");
        GetDataListData();
        initUI();
    }
    private void initUI() {
        listView.setAdapter(new NestFullListViewAdapter<AddressListData.AddressData>(R.layout.item_list_mine_address, addressdata) {
            @Override
            public void onBind(int position, final AddressListData.AddressData addressData, NestFullViewHolder holder) {
                holder.setText(R.id.item_list_mine_address_phone,addressData.getMobile());
                holder.setText(R.id.item_list_mine_address_name, addressData.getName());
                holder.setText(R.id.item_list_mine_address_address, addressData.getArea() + addressData.getAddress());
                if(isEdit) {
                    holder.setVisible(R.id.item_list_mine_address_check,true);
                    holder.setVisible(R.id.add_address_in,false);
                    if (addressData.getIs_default().equals("1")) {
                        holder.setChecked(R.id.item_list_mine_address_check, true);
                    } else {
                        holder.setChecked(R.id.item_list_mine_address_check, false);
                    }
                    listView.setOnItemClickListener(new NestFullListView.OnItemClickListener() {
                        @Override
                        public void onItemClick(NestFullListView nestFullListView, View view, int position) {
                            if(isEdit)
                            {
                                if (addressdata.get(position).getIs_default().equals("1")) {
                                    ObjectUtils.toast(mcontext, "已设置为默认地址");
                                } else {
                                    //设置默认地址
                                    setDefault(addressdata.get(position).getId());
                                }
                            }
                        }
                    });
                }
                else
                {
                    holder.setVisible(R.id.item_list_mine_address_check,false);
                    holder.setVisible(R.id.add_address_in,true);
                    holder.getView(R.id.edit_rl).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(mcontext, EditAddressActivity.class);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_ISEDIT, isEdit);
                            intent.putExtra("addressdetail_id",addressData.getId());
                            intent.putExtra("addressdetail_name",addressData.getMobile());
                            intent.putExtra("addressdetail_phone",addressData.getName());
                            intent.putExtra("addressdetail",addressData.getArea() + addressData.getAddress());
                            startActivityForResult(intent,0);
                        }
                    });
                }
            }
        });

    }

    //设置默认地址

    private void setDefault(String id) {

        String address = ConstantsUrl.domain+ ConstantsUrl.MEMBERADDRESSDEFAULT ;
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HashMap<String, String> map = new HashMap<>();
            map.put("id",id);
            HttpUtils.post(address, header, map, new dataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(data response, int id) {
                    if (response.getCode() == 0) {
                        ObjectUtils.toast(mcontext, "设置成功");
                        GetDataListData();
                    } else {
                        ObjectUtils.toast(mcontext, response.getMsg());
                    }
                }
            });
        } else {

            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {
            case 0:
                GetDataListData();
                break;
        }
    }

    @OnClick({R.id.main_top_back, R.id.address_add, R.id.address_add1, main_top_title_editaddress})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_top_back:
                finish();
                break;
            case main_top_title_editaddress:
                if(maintop_title_editaddress.getText().equals("编辑"))
                {
                    isEdit=false;
                    maintop_title_editaddress.setText("完成");
                }
                else
                {
                    isEdit=true;
                    maintop_title_editaddress.setText("编辑");
                }
                listView.updateUI();
                break;

            case R.id.address_add:
                Intent intent = new Intent(mcontext, EditAddressActivity.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_ISEDIT, isEdit);
                startActivityForResult(intent,0);
                break;
            case R.id.address_add1:
                Intent intent1 = new Intent(mcontext, EditAddressActivity.class);
                intent1.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_ISEDIT, isEdit);
                startActivityForResult(intent1,0);
                break;
        }
    }
    //获取地址列表数据
    private void GetDataListData() {

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERADDRESSLIST;
        MyLog.e("地址", address);

        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, null, new AddressListDataCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(AddressListData response, int id) {
                            addressdata.clear();
                            if (response.getCode() == 0) {
                                if (response.getData() != null) {
                                    noData.setVisibility(View.GONE);
                                    addressdata.addAll(response.getData());
                                    listView.updateUI();
                                } else {
                                    noData.setVisibility(View.VISIBLE);
                                }
                            } else {
                                if(response.getCode() == 40001){
                                    ObjectUtils.reLogin(mcontext);
                                }
                                noData.setVisibility(View.VISIBLE);

                            }

                        }
                    }

            );
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }


}
